package com.mes.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mes.cloud.dto.BmeBcJoinDTO;
import com.mes.cloud.service.BmeBcJoinService;
import com.mes.cloud.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * bme_bc_join基础表
 *
 * @author xhy
 * @version 1.0
 * @date 2019/05/09 16:00
 */
@Service
public class BmeBcJoinServiceImpl implements BmeBcJoinService {
    /**
     * 日志记录  其中在记录的时候 Class代表类名 Method代表方法名 Param代表参数
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(BmeBcJoinServiceImpl.class);

    @Autowired
    private com.mes.cloud.dao.BmeBcJoinDAO bmeBcJoinDAO;

    /**
     * 根据条件获取bme_bc_join基础表列表
     *
     * @param condition
     * @return List<BmeBcJoinDTO>
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public List<BmeBcJoinDTO> getBmeBcJoinList(Map<Object, Object> condition) {
        LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinList: InParam condition: {}", condition.toString());
        List<BmeBcJoinDTO> bmeBcJoinDTOList = bmeBcJoinDAO.getBmeBcJoinList(condition);
        if (bmeBcJoinDTOList == null) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinList: OutParam bmeBcJoinDTOList==null");
            return null;
        }
        LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinList: OutParam getBmeBcJoinList: {}", bmeBcJoinDTOList.toString());
        return bmeBcJoinDTOList;
    }

    /**
     * 获取bme_bc_join基础表的最大记录数
     *
     * @param condition
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long getBmeBcJoinCount(Map<Object, Object> condition) {
        LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinCount: InParam condition: {}", condition.toString());
        long count = bmeBcJoinDAO.getBmeBcJoinCount(condition);
        if (count == 0) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinCount: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinCount: OutParam count: {}", count);
        return count;
    }

    /**
     * 新增bme_bc_join基础表
     *
     * @param bmeBcJoinDTO
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long insertBmeBcJoin(BmeBcJoinDTO bmeBcJoinDTO) {
        LOGGER.info("Class BmeBcJoinServiceImpl: Method insertBmeBcJoin: InParam bmeBcJoinDTO: {}", bmeBcJoinDTO.toString());
        bmeBcJoinDTO.setId(UuId.getShortUuid());
        long count = bmeBcJoinDAO.insertBmeBcJoin(bmeBcJoinDTO);
        if (count == 0) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method insertBmeBcJoin: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcJoinServiceImpl: Method insertBmeBcJoin: OutParam count: {}", count);
        return count;
    }

    /**
     * 更新bme_bc_join基础表
     *
     * @param bmeBcJoinDTO
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long updateBmeBcJoin(BmeBcJoinDTO bmeBcJoinDTO) {
        LOGGER.info("Class BmeBcJoinServiceImpl: Method updateBmeBcJoin: InParam bmeBcJoinDTO: {}", bmeBcJoinDTO.toString());
        long count = bmeBcJoinDAO.updateBmeBcJoin(bmeBcJoinDTO);
        if (count == 0) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method updateBmeBcJoin: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcJoinServiceImpl: Method updateBmeBcJoin: OutParam count: {}", count);
        return count;
    }

    /**
     * 删除bme_bc_join基础表
     *
     * @param id
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long delBmeBcJoinById(String id) {
        LOGGER.info("Class BmeBcJoinServiceImpl: Method delBmeBcJoinById: InParam id: {}", id);
        long count = bmeBcJoinDAO.delBmeBcJoinById(id);
        if (count == 0) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method delBmeBcJoinById: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcJoinServiceImpl: Method delBmeBcJoinById: OutParam count: {}", count);
        return count;
    }

    /**
     * 根据bme_bc_join基础表标识获取bme_bc_join基础表
     *
     * @param id
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public BmeBcJoinDTO getBmeBcJoinById(String id) {
        LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinById: InParam id: {}", id);
        BmeBcJoinDTO bmeBcJoinDTO = bmeBcJoinDAO.getBmeBcJoinById(id);
        if (bmeBcJoinDTO == null) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinById: OutParam bmeBcJoinDTO==null");
            return null;
        }
        LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinById: OutParam bmeBcJoinDTO: {}", bmeBcJoinDTO);
        return bmeBcJoinDTO;
    }

    /**
     * 根据条件获取bme_bc_join基础表列表并进行分页查询
     *
     * @param condition
     * @return List<BmeBcJoinDTO>
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public CommPager<BmeBcJoinDTO> getBmeBcJoinListPage(Map<Object, Object> condition) {
        LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinListPage: InParam condition: " + condition.toString());
        int pageIndex = condition.get("pageIndex") == null ? 0 : Integer.valueOf(condition.get("pageIndex").toString());
        int pageSize = condition.get("pageSize") == null ? 10 : Integer.valueOf(condition.get("pageSize").toString());
        Page<BmeBcJoinDTO> page = PageHelper.startPage(pageIndex, pageSize, true);
        List<BmeBcJoinDTO> bmeBcJoinDTOList = bmeBcJoinDAO.getBmeBcJoinList(condition);
        if (bmeBcJoinDTOList == null) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinListPage: OutParam bmeBcJoinDTOList==null");
            return null;
        }
        LOGGER.info("Class BmeBcJoinServiceImpl: Method getBmeBcJoinListPage: OutParam bmeBcJoinDTOList: {}", bmeBcJoinDTOList.toString());
        CommPager<BmeBcJoinDTO> pageResult = new CommPager<>(page, bmeBcJoinDTOList);
        return pageResult;
    }

    /**
     * 对BC_Filed基础表标进行批量删除操作
     *
     * @param map
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    @Transactional
    public long batchDelete(Map<Object, Object> map) {
        LOGGER.info("Class BmeBcJoinServiceImpl: Method batchDelete: InParam map: {}", map);
        List<Long> longs = (List<Long>) map.get("ids");
        long count = bmeBcJoinDAO.batchDelete(longs);
        if (count == 0) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method batchDelete: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcJoinServiceImpl: Method batchDelete: OutParam count: {}", count);
        return count;
    }

    /**
     * 对bme_bc_join是否启用
     *
     * @param map
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long enable(Map<Object, Object> map) {
        if (map.get("id") == null || map.get("isEnable") == null) {
            LOGGER.info("Class BmeBcJoinServiceImpl: Method enable: map ==null");
            return 0;
        }
        return bmeBcJoinDAO.enable(map);
    }

    @Override
    public int importExcel(MultipartFile file) throws Exception {
        String rooPath = this.getClass().getResource("/").getPath();
        //复制文件到static目录
        String path = rooPath + "/static";
        String fileName = FileUploadUtils.uploadFile(file, path);
        String filePath = FileUploadUtils.uploadFile(file, path, fileName);
        FileInputStream fis = null;
        fis = new FileInputStream(filePath);
        // 创建工具类.
        ExcelUtil<BmeBcJoinDTO> util = new ExcelUtil<BmeBcJoinDTO>(BmeBcJoinDTO.class);
        List<BmeBcJoinDTO> paramList = util.importExcel(fileName, fis);
        HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();
        if (StringUtils.isEmpty(paramList)) {
            throw new Exception("数据为空！");
        }
        for (BmeBcJoinDTO bmeBcJoinDTO : paramList) {
            if (StringUtils.isEmpty(bmeBcJoinDTO.getId())) {
                bmeBcJoinDTO.setId(UuId.getShortUuid());
            } else {
                objectObjectHashMap.put("id", bmeBcJoinDTO.getId());
                long count = bmeBcJoinDAO.getBmeBcJoinCount(objectObjectHashMap);
                if (count > 0) {
                    throw new Exception("数据已经存在！");
                }
            }
        }
        return bmeBcJoinDAO.insertExcel(paramList);
    }

    @Override
    public void exportFile(Map<Object, Object> map, HttpServletResponse response) throws Exception {
        List<BmeBcJoinDTO> list = bmeBcJoinDAO.getBmeBcJoinList(map);
        if (list.size() == 0) {
            throw new Exception("导出数据为空！");
        }
        // 创建工具类
        ExcelUtil<BmeBcJoinDTO> util = new ExcelUtil<BmeBcJoinDTO>(BmeBcJoinDTO.class);
        // 导出
        String topName = DateUtil.getDateTime("yyyyMMddHHmmss");
        util.exportFile(list, DateUtil.getDateTime(topName), topName, response);
    }

    @Override
    public Long batchEnable(Map<Object, Object> map) throws Exception {
        if (map.get("ids") == null) {
            throw new Exception("ids不能为空");
        }
        return bmeBcJoinDAO.batchEnable((List<Long>) map.get("ids"));
    }
}
